<?php
// *****************************************************************************
//  Copyright 2007 AMC - Websource
//  Glenview, Illinois
//
//  File:           template.php
//  Author:         Mickey Freeman (mfreeman@connect2amc.com)
//  Function:       Class definition for template data, extends app_object
//
//  Created: 12/03/2007
//  Version: 1.0
//
//  Modified:
//
//
// ******************************************************************************

class template extends app_object
{
	/**
	 * Constructor, loads class defaults
	 *
	 * @return template
	 */
	function template()
	{
		$fields = array(
		        'project_id',
		        'template_name',
		        'template_start_flag',
		        'template_end_flag',
		        'template_add_date',
		        'template_status',
		        'module1_name',
		        'module1_html',
		        'module2_name',
		        'module2_html',
		        'module3_name',
		        'module3_html',
		        'module4_name',
		        'module4_html',
		        'module5_name',
		        'module5_html',
		        'module6_name',
		        'module6_html'

		);

		$table     = TEMPLATE_TABLE;
		$id_field  = 'template_id';
		parent::app_object($fields, $table, $id_field);

	}

	function validate_template()
	{
	$errors  = array();

	if($this->values['template_name']=='')
	  {
		$errors[] ='You must specify template name';

	  }
	if($this->values['template_start_flag']=='')
	  {
		$errors[] ='You must specify template start flag';

	  }
	  if($this->values['template_end_flag']=='')
	  {
		$errors[] ='You must specify template end flag';

	  }
	 return $errors;
    }
    function checkmodule($modulename,$modulecontent){

        $moduleid             = 0;
        $modulecontent        = str_replace('\n',' ',str_replace(chr(10),' ',str_replace(chr(13),' ',$modulecontent)));
        $sql                  = "select id from joomla_modules where title='".$modulename."'";
        $result               = app_data::mysql_query($sql);
        $res                  = mysql_num_rows($result);
        $content              = addslashes($modulecontent);
        if($res>0)
        {
         $idarr      = mysql_fetch_array($result);
         $moduleid   = $idarr['id'];
         //If module is already exist then this will update module content
         $upsql      = "update joomla_modules set params='custom_text=".$content."' where id=".$moduleid;
         $result     = app_data::mysql_query($upsql);
        }
        else
        {
        $insql      = "insert into joomla_modules(title,content,published,module,showtitle,params) VALUES('$modulename','',1,'mod_head',1,'custom_text=".$content."')";
        $result     = app_data::mysql_query($insql);
        $newid      = mysql_insert_id();
        $insql      = "insert into joomla_modules_menu(moduleid,menuid) VALUES('$newid','0')";
        $result     = app_data::mysql_query($insql);
        }

       return $result;
    }

	function save(){

		$this->values[template_add_date]  = time();

		$this->values[template_status]    = 1;
		parent::save();
		for($i=1;$i<7;$i++){
		    $modulename    = $this->values["module".$i."_name"];
		    $modulecontent = $this->values["module".$i."_html"];
		    if($modulename!='')
              {
		        $this->checkmodule($modulename,$modulecontent);
              }
		}
    }

	/**
	 * Retrieves all template type records from the database and returns them as an associative array
	 *
	 * @return arrayj
	 */
	function get_all_template()
	{
		$sql    = "SELECT * FROM " . TEMPLATE_TABLE . " where   	template_status=1 ORDER BY template_name";
		$result = app_data::mysql_query($sql);
		return $result;
	}

	function get_template($pro_id)
	{
		$sql          = "SELECT * FROM " . TEMPLATE_TABLE . " where template_id= ".$pro_id;
		$result       = app_data::mysql_query($sql);
		$result_arr   = mysql_fetch_assoc($result);
		return $result_arr;
	}

	function get_template_list()
	{
		$menu_arr  = array();
		$sql       = "SELECT * FROM " . TEMPLATE_TABLE . " where template_status=1 ORDER BY template_name";
		$result    = app_data::mysql_query($sql);
		if($result)
		{
			while($row = mysql_fetch_assoc($result))
			{
				$menu_arr[$row[template_id]] =  $row[template_name];

			}
		}
	    return $menu_arr;
	}
}